package pl.edu.mimuw.irs.rcp.controller.editor.workflow.handler;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.gef.commands.CommandStack;

import pl.edu.mimuw.irs.core.model.design.net.IRSNode;
import pl.edu.mimuw.irs.core.util.Converter;
import pl.edu.mimuw.irs.rcp.controller.editor.workflow.part.IRSNodeEditPart;

public abstract class AbstractMultipleNodesHandler extends AbstractCommandStackSelectionHandler {

	// *** Attributes

	private IRSNodeEditPart nodeEditPart;
	public IRSNodeEditPart getNodeEditPart() {
		return nodeEditPart;
	}
	public void setNodeEditPart(IRSNodeEditPart nodeEditPart) {
		this.nodeEditPart = nodeEditPart;
	}
	
	// *** Methods

	@Override
	public Object execute(ExecutionEvent event, CommandStack stack, Object[] selection) throws ExecutionException {
		if (selection.length < 1) return null;
		for (int i = 0; i < selection.length; i++) {
			if (!(selection[i] instanceof IRSNodeEditPart)) return null;
		}
		
		for (int i = 0; i < selection.length; i++) {
			IRSNodeEditPart nodeEditPart = (IRSNodeEditPart) selection[i];
			IRSNode node = Converter.asIRSNode(nodeEditPart.getNode());
		
			/*
			 * So we have one node in selection array, and a command stack.
			 * We can perform the command execution.
			 */
			execute(event, stack, node);
		}
		
		return null;
	}

	protected abstract Object execute(ExecutionEvent event, CommandStack commandStack, IRSNode node);
}
